﻿using System;
using System.Reflection;
using Glue.Internals.Nulls;

namespace Glue.Internals
{
    internal partial class AccessorFactory
    {
        private class MethodAccessor : IObjectMemberAccessor
        {
            private readonly MethodInfo methodInfo;

            public String Name { get; private set; }
            public bool CanGetValues { get { return true; } }
            public bool CanSetValues { get { return false; } }
            public Type PropertyType { get; private set; }

            public MethodAccessor(MethodInfo methodInfo)
            {
                this.methodInfo = methodInfo;
                Name = methodInfo.Name;
                PropertyType = methodInfo.ReturnParameter.ParameterType;
            }


            public Maybe InvokeGet(object toInvokeOn)
            {
                return methodInfo.Invoke(toInvokeOn, null).AsMaybe();
            }

            public void InvokeSet(object toInvokeOn, object toSet)
            {
                throw new GlueException("Can't InvokeSet on Method: " + Name);
            }

            public bool CanMapFrom(object thisObject) { return true; }
        }
    }
}
